<div class="layuimini-container layui-anim layui-anim-upbit">
    <div class="layuimini-main">
        <fieldset class="table-search-fieldset">
            <legend>搜索信息</legend>
            <div style="margin: 10px 10px 10px 10px">
                <form class="layui-form layui-form-pane" action="" id="form">
                    <div class="layui-form-item">
                        <div class="layui-inline">
                            <label class="layui-form-label">权限组名</label>
                            <div class="layui-input-inline">
                                <input type="text" name="permissionGroupName" autocomplete="off" class="layui-input"
                                    id="permissionGroupName">
                            </div>
                        </div>
                        <div class="layui-inline">
                            <button type="submit" class="layui-btn layui-btn-primary" lay-submit
                                lay-filter="data-search-btn" style="margin-left:10px;width:90px;height:38px;border:none;outline:none;border-radius:6px;background-color:#007bff;color:white"><i
                                    class="layui-icon"></i> 搜 索</button>
                        </div>
                    </div>
                </form>
            </div>
        </fieldset>
        <br>
        <div class="layui-btn-container" style="position: relative;">
            <!--<button type="submit" id="addPermissionGroup" class="layui-btn layui-btn-primary" lay-filter="data-add-btn"-->
            <!--style="width:110px;height:38px;background-color:#FFB800;border:none;outline:none;color:white"> +-->
            <!--新建权限组 </button>-->
            <button type="submit" id="addPermissionGroup" class="layui-btn layui-btn-warm layui-btn-radiusadd-dept" lay-filter="data-add-btn"
                        style="width:124px;height:38px;background-color:#FFB800;border:none;outline:none;color:white"><i class="layui-icon layui-icon-form"></i>添加权限组</button>
        </div>
        <table id="table" lay-filter="currentTableFilter"></table>
        <div id="page"></div>
    </div>
</div>

<script type="text/html" id="currentTableBar">

    {{# if(d.createor=="sys"){ }}
    <a class="layui-btn layui-btn-xs layui-btn layui-btn-normal data-count-edit" lay-event="edit" id="edit">编辑权限</a>
    {{# } else { }}
    <a class="layui-btn layui-btn-xs layui-btn layui-btn-normal data-count-edit" lay-event="edit" id="edit">编辑权限</a>
    <a class="layui-btn layui-btn-xs layui-btn-danger data-delete-btn"  lay-event="delete" id="delete">删除权限组</a>
    {{# } }}

</script>


<script>
    var tableBar = [
        { title: '序号', type: 'numbers', width: '10%', align: "center" },
        { field: 'permissionGroupName', title: '权限组名', width: '10%', align: "center" },
        { field: 'createor', title: '创建人', width: '20%', align: "center" },
        { field: 'showDepartments', title: '部门成员', width: '20%', align: "center" },
        { field: 'showUsers', title: '用户成员', width: '20%', align: "center" },
        { title: '操作', templet: '#currentTableBar', width: '20%', align: "center" }
    ]

    var initTableParams = {
        "permissionGroupName": "",
        "orderType": "ID",
        "orderValue": "asc",
        "page": 1,
        "pageSize": 10
    }

    layuiEx.tableRender(
        "/adminPermission/PermissionGrouplist",
        'table',
        'page',
        tableBar,
        initTableParams
    );


    //检索事件
    layui.form.on('submit(data-search-btn)', function (params) {
        layuiEx.tableRender(
            "/adminPermission/PermissionGrouplist",
            'table',
            'page',
            tableBar,
            {
                "permissionGroupName": params.field.permissionGroupName,
                "orderType": "ID",
                "orderValue": "asc",
                "page": 1,
                "pageSize": 10
            }
        );
        return false
    })



    // 监听添加操作
    $("#addPermissionGroup").on("click", function (obj) {
        loadAddPermissionPage("add")
    });

    // 监听编辑操作
    layui.table.on('tool(currentTableFilter)', function (obj) {
        var data = obj.data;
        
        if (obj.event === 'edit') {
            loadAddPermissionPage("edit",data)
        } else if (obj.event === 'delete') {
            layer.confirm('确定删除 ' + obj.data.permissionGroupName + " ?", function (index) {
                deleteDepartment(data)
            })
        }

    });


    function loadAddPermissionPage(addORedit,data){
        var content = layui.miniPage.getHrefContent('mypages/usual/permissionMgt/addPermission.html');
        var openWH = layui.miniPage.getOpenWidthHeight();
        var title = ""
        if (addORedit=="add") {
            title = "添加权限"
        }else{
            title = "编辑权限"
        }
        //不能用 var 前缀
        data_permissionMgt = data
        var index = layer.open({
            title: title,
            type: 1,
            shade: 0.2,
            maxmin: true,
            shadeClose: true,
            offset: '100px',
            area : [ '500px', 'auto' ],
            content: content,
            end: function () {
                var trunToPage = 1
                if (addORedit=="add") {

                }else{
                    trunToPage = $(".layui-laypage-em").next().html()
                }
                layuiEx.tableRender(
                            "/adminPermission/PermissionGrouplist",
                            'table',
                            'page',
                            tableBar,
                            {
                                "permissionGroupName": $("#permissionGroupName").val(),
                                "orderType": "ID",
                                "orderValue": "asc",
                                "page": trunToPage,
                                "pageSize": 10
                             }
                        );
            }
        });
        $(window).on("resize", function () {
            layer.full(index);
        });
        return false;
    }

    /*
    删除逻辑
    */
    function deleteDepartment(data){
        shared.ajaxPost('/adminPermission/deletePermissionGroup', { "permissionGroupID": data.permissionGroupID}, function callback(res) {
            if (res) {
                layer.msg(res, {
                    icon: 1,
                time: 1000 //2秒关闭（如果不配置，默认是3秒）
                }, function(){
                    layuiEx.tableRender(
                            "/adminPermission/PermissionGrouplist",
                            'table',
                            'page',
                            tableBar,
                            {
                                "permissionGroupName": $("#permissionGroupName").val(),
                                "orderType": "ID",
                                "orderValue": "asc",
                                "page": 1,
                                "pageSize": 10
                             }
                        );
                });
            }
        })
    }
</script>